"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.task = exports.serve = void 0;
const koa_1 = __importDefault(require("koa"));
const koa_bodyparser_1 = __importDefault(require("koa-bodyparser"));
const view_1 = require("./view");
const koa_views_1 = __importDefault(require("koa-views"));
const path_1 = __importDefault(require("path"));
const node_schedule_1 = __importDefault(require("node-schedule"));
function serve(config) {
    return __awaiter(this, void 0, void 0, function* () {
        console.log("serve服务开启。。。。。");
        const app = new koa_1.default();
        let { uri = 'mongodb://172.16.0.104:27017', db = 'faucet', port = 3000, tpl = './tpl' } = config.config;
        const cwd = process.cwd();
        app.use(koa_views_1.default(path_1.default.join(cwd, tpl), {
            extension: 'ejs'
        }));
        const faucetView = yield view_1.createfaucetView(config);
        app.use(koa_bodyparser_1.default());
        app.use(faucetView);
        const server = app.listen(port, () => {
            console.log(`服务启动，监听端口${port}`);
        });
    });
}
exports.serve = serve;
function task(config) {
    return __awaiter(this, void 0, void 0, function* () {
        console.log("task服务开启。。。。。");
        //每五分钟执行
        // insertHourData(config)
        // insertDailyData(config)
        // insertHourData(config)
        // insertDailyData(config)
        node_schedule_1.default.scheduleJob("0 4,9,14,19,24,29,34,39,44,49,54,59 * * * ?", () => {
            view_1.insertFiveData(config);
        });
        // 每小时执行
        node_schedule_1.default.scheduleJob("0 0 * * * ?", () => {
            view_1.insertHourData(config);
        });
        //每天0点执行
        node_schedule_1.default.scheduleJob("0 0 0 * * ?", () => {
            view_1.insertDailyData(config);
        });
    });
}
exports.task = task;
